<template>
  <el-dialog
    title="保存模板节目单"
    :before-close="closed"
    :visible.sync="innerVisible"
    width="420px"
    :close-on-click-modal="false">
    <el-form :model="templPgmForm" :rules="templPgmFormRules" ref="templPgmFormRef" >
      <el-form-item prop="templ_name">
        <el-input type="text" v-model="templPgmForm.templ_name" placeholder="请输入模板名称"></el-input>
      </el-form-item>
      <el-cascader-panel :options="templLists" :props="{ value: 'templ_id', label: 'templ_name' }" style="margin-top: 18px;" @change="templPgmChange"></el-cascader-panel>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="closed">取消</el-button>
      <el-button type="primary" @click="templPgmSvaeSubmit">确认</el-button>
      <el-button type="danger" @click="templDel">删除</el-button>
    </div>
  </el-dialog>
</template>

<script>
import pgmTemplApi from '@/api/makelist/pgmTempl.js' // 模板保存接口

export default {
  name: 'templpgm',
  data() {
    return {
      templPgmForm: {},
      templPgmFormRules: {
        templ_name: [
          { required: true, message: '请输入模板名称', trigger: 'blur' },
          { required: true, message: '请输入模板名称', trigger: 'change' }
        ]
      },
      templLists: []
    }
  },
  props: {
    innerVisible: {
      type: Boolean,
      default: false
    },
    pgmdateCurr: {
      type: Object,
      default: null
    }
  },
  created() {
    this.getTemplList() // 获取模板列表数据

    // 初始化模板添加表单字段
    this.templPgmForm.pgmlist_id = this.pgmdateCurr.value
    this.templPgmForm.channel_id = this.pgmdateCurr.pid
    this.templPgmForm.templ_id = 0
  },
  methods: {
    getTemplList() {
      pgmTemplApi.lists({
        channel_id: this.pgmdateCurr.pid
      }).then(res => {
        this.templLists = res.data.data
      })
    },
    closed() {
      this.$emit('update:innerVisible', false)
    },
    // 模板列表选择改变回调
    templPgmChange(value) {
      const rows = this.templLists.filter(item => value[0] === item.templ_id)[0]
      this.$set(this.templPgmForm, 'templ_name', rows.templ_name)
      this.$set(this.templPgmForm, 'templ_id', rows.templ_id)
    },
    // 模板添加提交
    async templPgmSvaeSubmit() {
      const rows = this.templLists.filter(item => (item.templ_id === this.templPgmForm.templ_id && item.templ_name === this.templPgmForm.templ_name))
      if (rows.length > 0) {
        const confirm = await this.$alert(`确定替换节目模板【${this.templPgmForm.templ_name}】`, '', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          showCancelButton: true,
          type: 'warning',
          closeOnClickModal: false
        }).catch(err => err)
        if (confirm === 'cancel') return false
      }

      // 验证模板form 并提交
      this.$refs.templPgmFormRef.validate(async valid => {
        if (!valid) return
        pgmTemplApi.add(this.templPgmForm).then(res => {
          if (res.data.data) {
            this.closed()
            return this.$message.success('模板保存成功！')
            // this.getTemplList() // 重新获取模板列表
          }
        }).catch(err => {
          console.log(err)
        })
      })
    },
    async templDel() {
      if (this.templPgmForm.templ_name === undefined || this.templPgmForm.templ_id === 0) {
        return this.$alert('请选择要删除的模板节目单！', '', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          showCancelButton: true,
          type: 'warning',
          closeOnClickModal: false
        }).catch(err => err)
      }
      console.log(this.templPgmForm)
      const confirm = await this.$alert(`确认删除节目模板【${this.templPgmForm.templ_name}】`, '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
        type: 'warning',
        closeOnClickModal: false
      }).catch(err => err)

      if (confirm === 'cancel') return
      // 删除接口
      pgmTemplApi.del([
        this.templPgmForm.templ_id
      ]).then(res => {
        if (res.data.meta.status === 200) {
          this.getTemplList() // 重新获取模板列表
          return this.$message.success(res.data.data.msg)
        }
      })
    }
  }
}
</script>

<style>
</style>
